import io
import random
import time
import urllib
import urllib2
import cookielib

from HTMLParser import HTMLParser
from HTMLParser import HTMLParseError

RECREATION_GOV_URL = 'http://www.recreation.gov'
url_search = '/unifSearch.do'
url_search_result = '/unifSearchResults.do'

class MyHTMLParser(HTMLParser):
    def __init__(self):
      HTMLParser.__init__(self)
      self.next_url = ''
      self.users = []
      self.stack = 0
      self.should_ignore = False

    def handle_starttag(self, tag, attrs):
        if tag == 'div':
          if ('class', 'name') in attrs or ('class', 'details') in attrs:
            self.stack += 1
            self.should_ignore = False
          else:
            self.should_ignore = True
          # for attr in attrs:
          #      print "     attr:", attr
        if tag == 'a':
          if ('class', 'load-next no-follow active') in attrs:
            for name, value in attrs:
              if name == 'href':
                self.next_url =  value

    def handle_endtag(self, tag):
        if tag == 'div':
          if self.stack and not self.should_ignore:
            self.stack -= 1

    def handle_data(self, data):
      if self.stack:
        self.users.append(data.strip(' \t\n\r'))

    def handle_data(self, data):
      if self.stack:
        self.users.append(data.strip(' \t\n\r'))

myfile = io.open('test', mode='a')

cookie1 = 'JSESSIONID=5CA3896E8549B64FAD94E39768096ABC.web02-ny; _rauv_=5CA3896E8549B64FAD94E39768096ABC.web02-ny_; NSC_QSPE-VXQSFD-IUUQ=ffffffff09293c2545525d5f4f58455e445a4a4225dd; __utma=76801942.53773301.1408784058.1408784058.1408784058.1; __utmb=76801942.28.9.1408785699719; __utmc=76801942; __utmz=76801942.1408784058.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); utag_main=_st:1408787499923$ses_id:1408784862807%3Bexp-session; fsr.s=%7B%22v2%22%3A-2%2C%22v1%22%3A-2%2C%22rid%22%3A%22de07553-78058712-86b2-eda6-5324c%22%2C%22to%22%3A2.1%2C%22c%22%3A%22http%3A%2F%2Fwww.recreation.gov%2FunifSearchResults.do%22%2C%22pv%22%3A17%2C%22lc%22%3A%7B%22d0%22%3A%7B%22v%22%3A17%2C%22s%22%3Atrue%7D%7D%2C%22cd%22%3A0%2C%22f%22%3A1408785699265%2C%22sd%22%3A0%7D; s_sess=%20s_cc%3Dtrue%3B%20SC_LINKS%3D%3B%20s_sq%3Danreserveamericaprod%253D%252526pid%25253Drecreation%252526pidt%25253D1%252526oid%25253DSearch%252526oidt%25253D3%252526ot%25253DSUBMIT%3B'

cookie = 'B93659C34D8CF26EE1A42DFDD367C86F.web10-ny_; fsr.r=%7B%22d%22%3A30%2C%22i%22%3A%22de07553-78679588-07d1-98d9-64b54%22%2C%22e%22%3A1409377851852%7D; JSESSIONID=CE0562205D2C9FE1E07F2241E5F134E5.web02-ny; __utma=76801942.1455266096.1408772679.1408777725.1408811091.3; __utmb=76801942.3.9.1408811119198; __utmc=76801942; __utmz=76801942.1408811091.3.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); utag_main=_st:1408812919331$ses_id:1408811832409%3Bexp-session; fsr.s=%7B%22v1%22%3A-2%2C%22v2%22%3A-2%2C%22rid%22%3A%22de07553-78679588-07d1-98d9-64b54%22%2C%22ru%22%3A%22https%3A%2F%2Fwww.google.com%2F%22%2C%22r%22%3A%22www.google.com%22%2C%22st%22%3A%22%22%2C%22to%22%3A2.8%2C%22c%22%3A%22http%3A%2F%2Fwww.recreation.gov%2FunifSearchResults.do%22%2C%22pv%22%3A2%2C%22lc%22%3A%7B%22d0%22%3A%7B%22v%22%3A2%2C%22s%22%3Afalse%7D%7D%2C%22cd%22%3A0%2C%22f%22%3A1408811119004%7D; s_sess=%20s_cc%3Dtrue%3B%20SC_LINKS%3D%3B%20s_sq%3Danreserveamericaprod%253D%252526pid%25253Drecreation%252526pidt%25253D1%252526oid%25253DSearch%252526oidt%25253D3%252526ot%25253DSUBMIT%3B; NSC_QSPE-VXQSFD-IUUQ=ffffffff09293c2545525d5f4f58455e445a4a4225dd'
cookie3 = 'JSESSIONID=602065593071E5B4237FD8D227685CD7.web04-ny; _rauv_=602065593071E5B4237FD8D227685CD7.web04-ny_; __utma=76801942.831968969.1408861144.1408861144.1408861144.1; __utmb=76801942.1.10.1408861144; __utmc=76801942; __utmz=76801942.1408861144.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); utag_main=_st:1408862944752$ses_id:1408862125559%3Bexp-session; fsr.s=%7B%22v2%22%3A1%2C%22v1%22%3A1%2C%22rid%22%3A%22de07553-78144703-89fb-83ff-83194%22%2C%22to%22%3A3%2C%22c%22%3A%22http%3A%2F%2Fwww.recreation.gov%2FunifSearch.do%22%2C%22pv%22%3A1%2C%22lc%22%3A%7B%22d0%22%3A%7B%22v%22%3A1%2C%22s%22%3Afalse%7D%7D%2C%22cd%22%3A0%2C%22mid%22%3A%22de07553-78145214-f79f-d73f-e22b3%22%2C%22rt%22%3Afalse%2C%22rc%22%3Afalse%7D; s_sess=%20s_cc%3Dtrue%3B%20SC_LINKS%3D%3B%20s_sq%3Danreserveamericaprod%253D%252526pid%25253Drecreation%252526pidt%25253D1%252526oid%25253DSearch%252526oidt%25253D3%252526ot%25253DSUBMIT%3B; NSC_QSPE-VXQSFD-IUUQ=ffffffff09293c2745525d5f4f58455e445a4a4225dd'
cookie4 ='_rauv_=602065593071E5B4237FD8D227685CD7.web04-ny_; JSESSIONID=602065593071E5B4237FD8D227685CD7.web04-ny; __utma=76801942.831968969.1408861144.1408861144.1408864816.2; NSC_QSPE-VXQSFD-IUUQ=ffffffff09293c2745525d5f4f58455e445a4a4225dd'
cookie5 = 'JSESSIONID=C0A33F22EDD6BA620701EFC3F4A78DA5.web05-ny; _rauv_=C0A33F22EDD6BA620701EFC3F4A78DA5.web05-ny_; NSC_QSPE-VXQSFD-IUUQ=ffffffff09293c2645525d5f4f58455e445a4a4225dd'
cookie55 = 'JSESSIONID=850F0BD2A0E4FAB3BC5AA0E1F657EE55.web08-ny; _rauv_=850F0BD2A0E4FAB3BC5AA0E1F657EE55.web08-ny_; NSC_QSPE-VXQSFD-IUUQ=ffffffff09293c2b45525d5f4f58455e445a4a4225dd'
cookie6 = 'JSESSIONID=DAA75B8D182538F3C186101827CC38E2.web02-ny; _rauv_=DAA75B8D182538F3C186101827CC38E2.web02-ny_; __utma=76801942.990438591.1409039912.1409039912.1409039912.1; __utmb=76801942.1.10.1409039912; __utmc=76801942; __utmz=76801942.1409039912.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); utag_main=_st:1409041712617$ses_id:1409040797889%3Bexp-session; fsr.s=%7B%22v2%22%3A1%2C%22v1%22%3A1%2C%22rid%22%3A%22de07553-78912634-8599-5a73-a3619%22%2C%22to%22%3A3%2C%22c%22%3A%22http%3A%2F%2Fwww.recreation.gov%2FunifSearch.do%22%2C%22pv%22%3A1%2C%22lc%22%3A%7B%22d0%22%3A%7B%22v%22%3A1%2C%22s%22%3Afalse%7D%7D%2C%22cd%22%3A0%2C%22mid%22%3A%22de07553-78913095-e635-78ac-8d7c1%22%2C%22rt%22%3Afalse%2C%22rc%22%3Afalse%7D; s_sess=%20s_cc%3Dtrue%3B%20SC_LINKS%3D%3B%20s_sq%3Danreserveamericaprod%253D%252526pid%25253Drecreation%252526pidt%25253D1%252526oid%25253DSearch%252526oidt%25253D3%252526ot%25253DSUBMIT%3B; NSC_QSPE-VXQSFD-IUUQ=ffffffff09293c2545525d5f4f58455e445a4a4225dd'

cookie66 = '_rauv_=DAA75B8D182538F3C186101827CC38E2.web02-ny_; JSESSIONID=DAA75B8D182538F3C186101827CC38E2.web02-ny; NSC_QSPE-VXQSFD-IUUQ=ffffffff09293c2545525d5f4f58455e445a4a4225dd'
headers = {
    'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language' : 'en-US,en;q=0.5',
    'Connection' : 'keep-alive',
    'Host' : 'www.recreation.gov',
    'Origin' : 'www.recreation.gov',
    'Referer' : 'www.recreation.gov/unifSearch.do',
    'Content-Type':'application/x-www-form-urlencoded',
     #'cookie' : cookie,
    'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0'
    }

post_params = { # member was blank, so I'm excluding it.
    'locationCriteria'  : 'YOSEMITE+NATIONAL+PARK%2C+YOSEMITE+VILLAGE%2C+CA+95389%2C+USA',
    'locationPosition' : 'NRSO%3A2991%3A-119.540833%3A37.839167%3A',
    'interest' : 'camping',
    'currentMaximumWindow':12,
    'campingDate':'Sat+Aug+30+2014',
    'campingDateFlex': '',
    'lengthOfStay': 2,
    'dayUseDate':'Sat+Aug+30+2014',
    'dayUseLengthOfStay': 2,
    'dayUseFlex': '',
    'dayuse_9001_3012': '',
    'dayuse_9001_218': '',
    'lookingFor': '',
    'camping_2001_3013': '',
    'camping_2001_218': '',
    'camping_2002_3013': '',
    'camping_2002_218': '',
    'camping_2003_3012': '',
    'camping_3100_3012': '',
    'camping_10001_3012': '',
    'camping_3101_3012': '',
    'camping_3101_218': '',
    'camping_9002_3012': '',
    'camping_9002_3013': '',
    'camping_9002_218': '',
    'camping_9001_3012': '',
    'camping_9001_218': '',
    'camping_3001_3013': '',
    'camping_2004_3013': '',
    'camping_2004_3012': '',
    'camping_3102_3012': ''
    }

urllib2.HTTPSHandler(debuglevel=1)
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

opener.addheaders.append(('User-agent', 'Mozilla/5.0'))
x = opener.open(RECREATION_GOV_URL + url_search)
print x.read()

cookies = []
#cookies =['JSESSIONID']
for cookie in cj:
   cookies.append(str(cookie).split(" ")[1])
new_cookies = "; ".join(cookies)
print new_cookies

#opener.addheaders.append(('cookie', cookie66))
opener.addheaders.append(('cookie', new_cookies))
request = urllib2.Request(
   RECREATION_GOV_URL + url_search_result,
   urllib.urlencode(post_params))

f = opener.open(request)
#print f.read()

#f = opener.open(RECREATION_GOV_URL + url_search_result,
#                urllib.urlencode(post_params))

#                      headers)
#req = urllib2.Request(complete_url,
#                      urllib.urlencode(post_params),
#                      headers)

# f = urllib2.urlopen(req)
#content = f.read()
#print content
#content = content[10:-1]
#parser = MyHTMLParser()
#try:
#parser.feed(content.decode('utf-8'))
#except HTMLParseError as e:
#print "Parser error" + e.msg
#continue
#print parser.users
#myfile.write(u'\n'.join(parser.users))
#myfile.write(u'\n')
#myfile.flush()
#url = parser.next_url
#print url
#time.sleep(random.randint(1,3))
